﻿using System;
using System.Collections.Generic;
using System.Windows;
using Custom.Windows;

namespace ZenBridge
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : InstanceAwareApplication
    {
        public static Queue<string> CommandLineArgsQueue = new Queue<string>();

        public event EventHandler NewFileQueued;

        public void OnNewFileQueued()
        {
            if (NewFileQueued != null)
            {
                NewFileQueued.Invoke(this, new EventArgs());
            }

            if ( (App.Current.MainWindow != null) && (!App.Current.MainWindow.IsActive) )
            {
                App.Current.MainWindow.Activate();
            }
        }

        public App()
            : base(ApplicationInstanceAwareness.UserSession)
        {
        }

        /// <summary>
        /// Raises the <see cref="E:Startup"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.Windows.StartupEventArgs"/> instance containing the event data.</param>
        /// <param name="isFirstInstance">If set to <c>true</c> the current instance is the first application instance.</param>
        protected override void OnStartup(StartupEventArgs e, bool isFirstInstance)
        {
            base.OnStartup(e, isFirstInstance);

            foreach (string arg in e.Args)
            {
                CommandLineArgsQueue.Enqueue(arg);
            }
            OnNewFileQueued();
            
            if (!isFirstInstance)
            {                
                Shutdown(1);
            }
        }

        /// <summary>
        /// Raises the <see cref="E:StartupNextInstance"/> event.
        /// </summary>
        /// <param name="e">The <see cref="PowersoftSDK.Windows.StartupNextInstanceEventArgs"/> instance containing the event data.</param>
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e)
        {
            base.OnStartupNextInstance(e);

            foreach (string arg in e.Args)
            {
                CommandLineArgsQueue.Enqueue(arg);
            }
            OnNewFileQueued();
        }
    }
}
